package com.cmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmj.Utils.BeanCopyUtils;
import com.cmj.dao.LinkMapper;
import com.cmj.domain.ResponseResult;
import com.cmj.domain.SystemConstants;
import com.cmj.domain.dto.LinkDto;
import com.cmj.domain.entity.Link;
import com.cmj.domain.vo.LinkVo;
import com.cmj.domain.vo.PageVo;
import com.cmj.service.LinkService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import static com.cmj.domain.SystemConstants.Link_STATUS_PAST;
import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2022-03-13 15:48:08
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    @Resource
    private LinkMapper linkMapper;

    /***
     * 获取所有友链 （已经审核通过） status=0
     * @return 审核通过的友链
     */
    @Override
    public ResponseResult getAllLink() {
        //审核通过的友链
        List<Link> linkList = linkMapper.getAllLink(Link_STATUS_PAST);
        //封装到vo中
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult<PageVo> getAllLink(Integer pageNum, Integer pageSize, LinkDto linkDto) {
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件 字段名 值 boolean condition, R column, Object val
        linkLambdaQueryWrapper.eq(StringUtils.hasText(linkDto.getName()),Link::getName,linkDto.getName());
        linkLambdaQueryWrapper.eq(StringUtils.hasText(linkDto.getStatus()),Link::getStatus,linkDto.getStatus());

        Page<Link> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,linkLambdaQueryWrapper);
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult delLink(Long[] id) {
        int i = linkMapper.deleteBatchIds(Arrays.asList(id));
        if (i == 0){
            new RuntimeException(SystemConstants.DELETE_FAIL);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<LinkVo> getLink(Long id) {
        Link link = linkMapper.selectById(id);
        if (isEmpty(link)){
            new RuntimeException(SystemConstants.SELECT_FAIL);
        }
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return ResponseResult.okResult(linkVo);
    }

    @Override
    public ResponseResult updateLink(Link link) {
        QueryWrapper<Link> linkQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Link> id = linkQueryWrapper.eq("id", link.getId());
        int result = linkMapper.update(link, id);
        if (result == 0){
            new RuntimeException(SystemConstants.UPDATE_FAIL);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<LinkVo> addLink(Link link) {
        int result = linkMapper.insert(link);
        if (result == 0){
            new RuntimeException(SystemConstants.INSERT_FAIL);
        }
        return ResponseResult.okResult();
    }


}
